package com.ve.view.editor;

import com.ve.view.editor.out.SpanInterface;
import com.ve.view.editor.span.TextSpanner;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class SpanManager extends Base implements SpanInterface {
    private boolean spanning;

    private TextSpanner textSpanner;
    private ExecutorService executorService=Executors.newSingleThreadExecutor();

    @Override
    public void setTextSpanner(TextSpanner textSpanner) {
        this.textSpanner = textSpanner;
    }


    public SpanManager(Editor editor) {
        super(editor);
    }

    @Override
    public void startSpan() {
        if (textSpanner == null) {
            return;
        }
        executorService.execute(() -> {
            spanning = true;
            textSpanner.startSpan(document);
            painter.setTextSpanData(textSpanner.getSpanData());
            editor.postInvalidate();
            spanning = false;
        });
    }

    @Override
    public boolean isSpanning() {
        return spanning;
    }

    @Override
    public void stopSpan() {
        spanning = false;
        if (textSpanner != null) {
            textSpanner.stopSpan();
        }
    }
}
